﻿using System.Data;
using SemiCASE.DataAccess;
using SemiCASE.DataType.Common.Attributes;
using System;

namespace SemiCASE.Business
{
    [ServiceConnectionNameAttribute("SemiCASEConnectionString")]
    public class ExtentionStepBS : BusinessBase
    {
        public DataTable LoadExtentionStepsFromDB(Guid pProjectID, Guid pUseCaseID)
        {
            DataTable dt = new DataTable();
            IData data = GetDataObject();
            string sqlText = @"WITH PROJECT_EXTENTIONS AS
                            (
	                            SELECT DISTINCT 
			                          ExtentionID
	                            FROM [SemiCaseDB].[dbo].[RELATION]
	                            WHERE ProjectID = @ProjectID AND UseCaseID = @UseCaseID
                            )

                             SELECT
	                              ES.[StepID]
                                  ,ES.[ExtentionID]
                                  ,ES.[SiraNo]
                                  ,ES.[StepName]
                              FROM [SemiCaseDB].[dbo].[EXTENTION_STEP] AS ES
                                    INNER JOIN PROJECT_EXTENTIONS AS PE ON ES.ExtentionID = PE.ExtentionID
                              ORDER BY ES.SiraNo";

            data.AddSqlParameter("ProjectID", pProjectID, SqlDbType.UniqueIdentifier);
            data.AddSqlParameter("UseCaseID", pUseCaseID, SqlDbType.UniqueIdentifier);
            data.GetRecords(dt, sqlText);
            return dt;
        }

        public void CreateExtentionSteps(DataTable dtExtentionSteps)
        {
            if (dtExtentionSteps == null)
                return;

            foreach (DataRow row in dtExtentionSteps.Rows)
            {
                IData data = GetDataObject();
                string sqlText = @"INSERT INTO [SemiCaseDB].[dbo].[EXTENTION_STEP]
                                       ([StepID]
                                       ,[ExtentionID]
                                       ,[SiraNo]
                                       ,[StepName])
                                 VALUES
                                       (@StepID
                                       ,@ExtentionID
                                       ,@SiraNo
                                       ,@StepName)";

                data.AddSqlParameter("StepID", row["StepID"], SqlDbType.UniqueIdentifier);
                data.AddSqlParameter("ExtentionID", row["ExtentionID"], SqlDbType.UniqueIdentifier);
                data.AddSqlParameter("SiraNo", row["SiraNo"], SqlDbType.Int);
                data.AddSqlParameter("StepName", row["StepName"], SqlDbType.VarChar);

                data.ExecuteStatement(sqlText);
            }
        }
    }
}
